package cn.training.controller;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.training.bean.UserBean;
import cn.training.service.HelloWorldService;

@Controller("HelloWorldController")
@RequestMapping("/")
public class HelloWorldController {
	@Autowired
	HelloWorldService helloWorldService;

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String initLogin(Model model) {
		UserBean userBean =new UserBean();
		model.addAttribute("userBean", userBean);
		return "helloWorld";
	}

	@RequestMapping(value = "/init", method = RequestMethod.POST, params = "login")
	public String initLogin( Model model,@Valid @ModelAttribute("userBean")UserBean userBean,BindingResult results) {
		if(results.hasErrors())
		{
			return "helloWorld";
		}		
		UserBean result = helloWorldService.searchUser(userBean);
		
		if (result != null) {
			List<UserBean> result1 = helloWorldService.searchUserss();
			model.addAttribute("list", result1);
			return "login";
		} else {
			model.addAttribute("message", "用户名或密码错误！");
			return "helloWorld";
		}

	}

	@RequestMapping(value = "/init", method = RequestMethod.POST, params = "register")
	public String initregister(UserBean userBean, Model model) {
		// int update=helloWorldService.update(userBean);
		// int insert=helloWorldService.insert(userBean);
		// UserBean result = helloWorldService.searchUser(userBean);
		// model.addAttribute("userBean", result);
		return "register";
	}

	@RequestMapping(value = "/initadd", method = RequestMethod.POST, params = "zhuce")
	public String initzhuce( Model model,@Valid@ModelAttribute("userBean")UserBean userBean,BindingResult results) {
		if(results.hasErrors())
		{ 
			return "register";
		}		
		String pass1 = userBean.getUserPassword();
		String pass2 = userBean.getRePassword();
		if (pass1.equals(pass2)) {
			int insert = helloWorldService.insert(userBean);
			if (insert == 1)
			{
				model.addAttribute("message", "注册成功！");
				return "sure";
			} else {
				model.addAttribute("message", "注册失败！");
				model.addAttribute("userBean", userBean);
				return "register";
			}
		} else {
			model.addAttribute("message", "两次输入的密码不一致！");
			model.addAttribute("userBean", userBean);
			return "register";
		}

	}

	@RequestMapping(value = "/initadd", method = RequestMethod.POST, params = "reset")
	public String initreset(UserBean userBean, Model model) {
		// int update=helloWorldService.update(userBean);
		// int insert=helloWorldService.insert(userBean);
		// UserBean result = helloWorldService.searchUser(userBean);
		// model.addAttribute("userBean", result);
		return "helloWorld";
	}

	@RequestMapping(value = "/del", method = RequestMethod.GET)
	public String del(UserBean userBean, Model model) {
		// int update=helloWorldService.update(userBean);
		// int insert=helloWorldService.insert(userBean);
		//int result = helloWorldService.deleteUser(userBean);
		List<UserBean> refresh = helloWorldService.searchUsers(userBean);
		model.addAttribute("list", refresh);
		return "sureDelete";
	}
	@RequestMapping(value = "/delete", method = RequestMethod.POST,params = "sureDel")
	public String suredelete(UserBean userBean, Model model) {
		// int update=helloWorldService.update(userBean);
		// int insert=helloWorldService.insert(userBean);
		int result = helloWorldService.deleteUser(userBean);
		List<UserBean> refresh = helloWorldService.searchUserss();
		model.addAttribute("list", refresh);
		return "login";
	}
	@RequestMapping(value = "/delete", method = RequestMethod.POST,params = "noDel")
	public String nodelete(UserBean userBean, Model model) {
			
		List<UserBean> result = helloWorldService.searchUserss();
		model.addAttribute("list", result);
		return "login";
	}

	 @RequestMapping(value = "/updates", method = RequestMethod.GET)
	 public String updateLogin(UserBean userBean,Model model) {	
	 return "xiugai";
	 }
	 @RequestMapping(value = "/update", method = RequestMethod.POST)
	   public String update(Model model,@Valid@ModelAttribute("userBean")UserBean userBean,BindingResult results) {
	    if(results.hasErrors())
	    {
	 	  return "xiugai";
	    }
		 int result=helloWorldService.update(userBean);	
	//UserBean result1 = helloWorldService.searchUser(userBean);
	// model.addAttribute("userBean", result1);
	if(result==1)
	{
		List<UserBean> result1 = helloWorldService.searchUserss();
		model.addAttribute("list", result1);
		return "login";
 }
	 else
	{
	 model.addAttribute("message","更新失败！");
	 model.addAttribute("userBean", userBean);
	 }
	 return "login";

	 }
}
     

    		

